﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.mMall.Buyer
{
    public partial class mfOrderInfo : msBsc.Cls.mbPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void getOrder()
        {
            string orderId = this.GetParamHtValue("orderId");
            if (string.IsNullOrEmpty(orderId))
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizDJkOrdersDetail orderInfo = new AppLogic.DataAlc.CBizDJkOrdersDetail())
            {
                DataSet ds = orderInfo.GetOrderDetailsById(orderId, false);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                string ret = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "ORDERS_INFO_ID,ORDER_TIME,ORDER_CODE,ORDER_STATUS,ORDER_START_TIME,ORDER_STOP_TIME,ORDER_TOTAL_BALANCE,DISCOUNT_AMT,PAID_AMT,CUSTOMER_REGISTER_ID,CUSTOMER_NICK,CUSTOMER_DEFAULT_PIC,DEPOSIT_RATIO,PAYABLE_AMT,PAYABLE_TOTAL_AMT", 1, 100, "");
                this.ExecJScript("setOrder(" + ret + ")");
            }
        }

        //取消订单
        private void cancelOrder()
        {
            string orderId = this.GetParamHtValue("orderId");
            if (string.IsNullOrEmpty(orderId))
            {
                this.Alert("请选择订单");
                return;
            }
            string djakcId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(djakcId))
            {
                //页面跳转
                this.Alert("请登录");
                this.ExecJScript(@"window.location.reload()");
            }
            using (msBsc.AppLogic.DataAlc.CBizOrdersInfo orderInfo = new AppLogic.DataAlc.CBizOrdersInfo())
            {
                string[] ret = orderInfo.UpdateBuyerOrderStatus(djakcId, orderId, msBsc.AppLogic.DataAlc.OrderStatusEnum.CancelOrder);
                if (ret[0] != "true")
                {
                    this.Alert(ret[1]);
                }
                else
                {
                    //页面跳转
                    this.Alert("取消成功");
                    this.ExecJScript(@"window.location.reload()");
                }
            }
        }

        //完成订单
        private void completeOrder()
        {
            string orderId = this.GetParamHtValue("orderId");
            if (string.IsNullOrEmpty(orderId))
            {
                this.Alert("请选择订单");
                return;
            }
            string djakcId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(djakcId))
            {
                //页面跳转
                this.Alert("请登录");
                this.ExecJScript(@"window.location.reload()");
            }
            using (msBsc.AppLogic.DataAlc.CBizDjkChargeTradingLog chargeLog = new AppLogic.DataAlc.CBizDjkChargeTradingLog())
            {
                string charRet = chargeLog.ManualCloseAccount(orderId);
                if (charRet != "OK")
                {
                    this.Alert(charRet);
                    return;
                }
                this.ExecJScript(@"window.location.reload()");
            }
        }

        //
        private void toPay()
        {
            using (msBsc.AppLogic.DataAlc.CBizDjkChargeTradingLog chargeLog = new AppLogic.DataAlc.CBizDjkChargeTradingLog())
            {
                string orderId = this.GetParamHtValue("orderId");
                if (string.IsNullOrEmpty(orderId))
                {
                    this.Alert("请选择订单");
                    return;
                }
                string[] ret = chargeLog.AddCustomerTradingForPay(orderId);
                if (ret[0] == "OK")
                {
                    this.ExecJScript(@"window.location.href = '/wAlipay/wmdefault.aspx?ID=" + ret[1] + "'");
                }
                else
                {
                    this.Alert(ret[1]);
                }
            }
        }

        /// <summary>
        /// 删除订单
        /// </summary>
        /// Author:任延华
        /// Creation date:2015-07-27
        private void deleteOrder() 
        {
            string orderId = this.GetParamHtValue("orderId");
            if (string.IsNullOrEmpty(orderId))
            {
                this.Alert("请选择订单");
                return;
            }
            string djakcId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(djakcId))
            {
                //页面跳转
                this.Alert("请登录");
                this.ExecJScript(@"window.location.reload()");
            }
            using (msBsc.AppLogic.DataAlc.CBizOrdersInfo orderInfo = new AppLogic.DataAlc.CBizOrdersInfo())
            {
                string ret = orderInfo.DeleteOrder(djakcId,orderId,false);
                if (ret != "OK")
                {
                    this.Alert(ret);
                }
                else
                {
                    //页面跳转
                    this.Alert("删除成功");
                    this.ExecJScript(@"window.location.href = '/mb_OrderList.htm'");
                }
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                getOrder();
            }
            else if (this.strAction == "completeOrder")
            {
                completeOrder();
            }
            else if (this.strAction == "cancelOrder")
            {
                cancelOrder();
            }
            else if (this.strAction == "toPay")
            {
                toPay();
            }
            else if (this.strAction == "deleteOrder")
            {
                deleteOrder();
            }
        }
    }
}